package com.server.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.common.chart.ColumnVectorV2;
import com.server.service.IMonthStatisticService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

@Controller
@RequestMapping("month")
public class MonthStaitisticController {

	@Autowired
	private IMonthStatisticService monthStatisticService;
	
	
	@RequestMapping("")
	public ModelAndView orgPage(){
		ModelAndView mv = new ModelAndView("userCenter/month/statistic");
		mv.addObject("projectName",ProjectInfor.PLATFORM_NAME);
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	@RequestMapping("period")
	public void period(HttpServletRequest request,HttpServletResponse response){
		String cardId = SessionUtil.getStringAttri(request, SessionData.cardIdKey);
		List<Map<String, Object>> period = monthStatisticService.getPeriod(cardId);
		OutputUtil.returnString(response, JSONSerielizer.toJson(period, null, null));
	}
	@RequestMapping("chartData")
	public void chartData(HttpServletRequest request,HttpServletResponse response){
		String cardId = SessionUtil.getStringAttri(request, SessionData.cardIdKey);
		String startPeriod = request.getParameter("startPeriod");
		String endPeriod = request.getParameter("endPeriod");
		ColumnVectorV2 userStatictics = monthStatisticService.getStatictics(cardId, startPeriod, endPeriod);
		String[] exclude = new String[] { "*.class" };
		OutputUtil.returnString(response, JSONSerielizer.toJson(userStatictics,null,exclude));
	}
	@RequestMapping("list/{start}/{end}")
	public void list(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String start,
			@PathVariable String end){
		String cardId = SessionUtil.getStringAttri(request, SessionData.cardIdKey);
		DataVector<Map<String, Object>> listData = monthStatisticService.getList(new RequestInput(request),cardId,start,end); 
		OutputUtil.returnString(response, JSONSerielizer.toJson(listData.getList(), null, null));
	}
}
